/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG                        *
*Copyright (C) 2006, Tony Sparks                                                     *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/

#pragma once

#include <string>
#include <map>
#include <stack>
#include "../EventManager.h"


/*-----------------------------------------------
    Status of the mission
------------------------------------------------*/
enum MissionStatus {
    M_INCOMPLETE,
    M_COMPLETE,
    M_FAILED,
    M_REWARDED,
    M_IMPOSSIBLE_TO_COMPLETE

};


/**
====================================
Event for adding Sending a mission to a mission holder
====================================
*/
class Mission;
struct MissionEvent : public IEventData
{   MissionEvent(Mission* m, unsigned int id) { m_mission = m; ent_id = id; };
    Mission* m_mission;
    unsigned int ent_id;
    IEventData* Copy() { return new MissionEvent(m_mission, ent_id); };
    virtual ~MissionEvent() {IEventData::~IEventData(); };
};

/**
============================
A Mission is a Task given to the player
in which he can fulfull it - retrieve a 
reword.
============================
*/
class Mission
{
public:
    Mission(void) : m_status(M_INCOMPLETE) {};

    /** Mission name */
    std::string GetName() { return m_name; };
    void SetName(const std::string &n) { m_name = n; };

    /** Get the Status of the mission */
    int GetStatus() { return m_status; };
    /** Set the status of this mission */
    void SetStatus(int s) { m_status = s; };

    /** Set the rules (a lua script file */
    void SetRules( const std::string &file ) { m_rules = file; };
    std::string GetRules() { return m_rules; };
    /**
     * A text description of this mission
     */
    std::string GetObjective() { return m_objective; };
    void SetObjective(const std::string &o ) { m_objective = o; };
private:
    // status
    int m_status;
    // description
    std::string m_objective;
    // name of the mission
    std::string m_name;

    // rules for completion
    std::string m_rules;
public:
    virtual ~Mission(void) {};
};

/**
============================
Contains the status of Missions
============================
*/
class MissionHolder : public IEventListener
{
public:
    MissionHolder(void);

    /** Add a mission to the bin */
    void AddMission(Mission* m) { m_missions[m->GetName()] = m; };
    /** Mission is completed */
    void CompleteMission(const std::string &misssion);
    /** Failed a mission */
    void FailedMission(const std::string &misssion);
    /** Retrieve the status of a mission */
    int GetMissionStatus(const std::string &misssion);
    /** Has mission */
    bool HasMission( const std::string &mission);
    /** Get a Mission */
    Mission* GetMission( const std::string &mission);

    /*---------------------------------------------
        Receive an Event from the MissionDispatcher
        The MissionDispatcher belongs to an IEntity
        who can give out Missions.  The MissionDispatcher
        recieves Events stating to Peek its next Mission.
        Once that mission have been completed, we Pop it
        from the MissionDispatcher Stack.
    ----------------------------------------------*/

    /* Get the Name of the Listener */
    std::string GetName() { return "missionholder"; };
    /** Get Listener Type - Events this listener listens too */
    int GetListenerType() { return LT_MISSION_HOLDER; };

    /** Handle an event */
    bool HandleEvent( IEvent* e );

private:
    /** Remove a mission */
    void RemoveMission( Mission* m );
    /** Remove all */
    void RemoveAll();
    // hash of missions
    typedef std::map<std::string, Mission*> type_Missions;
    type_Missions m_missions;

    // Temp for now - the player is the only one
    // who can get Missions - TODO - Multiplayer?
    unsigned int m_id;

public:
    virtual ~MissionHolder(void);
};


/**
======================================
Dispatch Missions
======================================
*/
class MissionDispatcher : public IEventListener
{
public:
    MissionDispatcher();

    /** Create a mission */
    Mission* CreateMission( const std::string &file );
    /** Get the next mission */
    Mission* GetNextMission();
    /** Has completed a mission */
    bool HasCompleted();
    /** Check to see if this mission was already assigned */
    bool IsAssigned(const std::string &mission );
  /*---------------------------------------------
        Receive an event to see which mission to
        dispatch to the MissionHolder
    ----------------------------------------------*/

    /* Get the Name of the Listener */
    std::string GetName() { return "missiondispatcher"; };
    /** Get Listener Type - Events this listener listens too */
    int GetListenerType() { return LT_MISSION_DISPATCHER; };

    /** Handle an event */
    bool HandleEvent( IEvent* e );

private:
    // stack of missions to give
    typedef std::map<std::string, Mission*>   type_Missions;
    type_Missions m_assignedMissions;
public:
    virtual ~MissionDispatcher();

};